<?php
namespace app\api\controller\account;

use app\api\controller\Base;
use app\api\controller\Pub;
use app\common\model\account\AccountEntrustUse;
use app\common\model\company\Company;
use app\common\model\user\User as UserModel;

//产废单位 产生台账
class Aentrustuse extends Base
{
    public function initialize()
    {
        parent::initialize();
        self::is_login();
        // Rolepower::powerguard(801001);
    }

    //获取基础信息
    //字段信息：产生部门经办人 去向 运送经办人 贮存经办人 产废设施列表 贮存设施编码 贮存设施类型
    //数据信息：最近的危废代码 、 上一次台账信息

    // 创建产生台账
    public function create()
    {
        $data        = input('post.');
        $CreatUserId = Pub::userid();
        if (empty($data['list'])) {
            return myerr('没有找到台账列表');
        }
        $data['list'] = json_array($data['list'], true);
        $nlist        = []; //新建列表
        $filelist     = ['GoodId', 'GoodName', 'AccountTime', 'UpNote', 'Quantity', 'BoxType', 'BoxCount', 'BoxBatchCode', 'OrderListId', 'CreateMachineCode', 'CreateUserName']; //产废字段
        //委外利用字段
        $entrustusefile = ['UseType','Type','AcceptingUnitType', 'AcceptingUnitName', 'AcceptingUnitCode', 'ExemptionUnitName', 'AbroadUnitName', 'AbroadUnitCode', 'SourceBatchCode'];
        $company        = Company::user_getinfo(); //获取产废单位企业信息
        if (empty($company)) {
            return myerr('缺少企业信息');
        }
        $company_id = $company->CompanyId;
        foreach ($data['list'] as $key => $value) {
            $new['CompanyId'] = $company['CompanyId'];
            //组合产废台账信息
            $new['BatchCode']   = AccountEntrustUse::getBatchCode($company_id,$value['AccountTime'],$value['Type']);
            // $new['EndFlag']     = $endflag;
            $new['CreatUserId'] = $CreatUserId;
            $new['UpdateTime']  = date('Y-m-d H:i:s', time());
            $new['Unit']        = '公斤';
            foreach ($filelist as $fk => $fv) {
                $new[$fv] = empty($value[$fv]) ? '' : trim($value[$fv]);
            }
            foreach ($entrustusefile as $fk => $fv) {
                $new[$fv] = empty($value[$fv]) ? '' : trim($value[$fv]);
            }
            //创建产废台账
            $new['OutWarehouseId'] = AccountEntrustUse::createAccount($new);
        }
        return myok('创建成功');
    }

     /**
     * 获取基础信息
     * @return [type] [description]
     */
    public function getcreateinfo()
    {
        $data = input('get.');
        $company =  Company::user_getinfo();//获取产废单位企业信息
        if(empty($company)){
            return myerr('缺少企业信息');
        }
        $company_id = $company->CompanyId;
        $data = AccountEntrustUse::where('CompanyId',$company_id)->order('Id desc')->with('goodinfo')->limit(10)->hidden(['OperationLog','goodinfo.OperationLog','goodinfo.TotalWeight'])->select();
        $ls = [];
        if(!empty($data)){
            $ls = AccountEntrustUse::where('CompanyId',$company_id)->where('AccountTime',$data[0]['AccountTime'])->with('goodinfo')->hidden(['OperationLog','goodinfo.OperationLog','goodinfo.TotalWeight'])->select();
        }
        return myok(['list'=>$data,'last'=>$ls]);
    }


    /**
     * 修改/完善企业信息
     * 产废人信息也再次完善
     * @return [type] [description]
     */
    public function reinfo()
    {
        $data    = input('post.');
        $company = Coll::user_getinfo();
        //为空则给企业返回修改信息
        if (empty($data)) {
            return myok($company);
        }
        if (empty($company)) {
            //创建一个企业信息
            $result = $this->validate($data, 'app\common\validate\Collection');
            if (true !== $result) {
                return myerr($result);
            }
            $company = Coll::user_create($data);
            return myok('财务信息已登记');
        } else {
            //修改企业信息
            $sb = Coll::user_reinfo($data);
            return myok('财务信息修改成功');
        }
    }

    /**
     * 判断账号是否登录
     * @return boolean [description]
     */
    private function is_login()
    {
        $data = UserModel::is_sessionlogin();
        if (!$data['code']) {
            $data['options'] = 'relogin';
            json($data)->send();
            exit;
        }
    }
}
